<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-17px"
      @submit.prevent="handleQuery"
    >
      <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          class="!w-200px"
          placeholder="请输入项目编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          class="!w-200px"
          placeholder="请输入项目名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变更角色" prop="changeRole">
        <el-select
          v-model="queryParams.changeRole"
          class="!w-200px"
          placeholder="请选择变更角色"
          @change="handleQuery"
          placement="bottom"
        >
          <el-option
            v-for="item in stageList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目角色" prop="projectRole">
        <el-select
          v-model="queryParams.projectRole"
          class="!w-200px"
          placeholder="请选择项目角色"
          @change="handleQuery"
          placement="bottom"
        >
          <el-option
            v-for="item in roleList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table
      :data="list"
      v-loading="loading"
      empty-text="暂无记录"
      element-loading-text="Loading..."
      stripe
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      show-overflow-tooltip
    >
      <el-table-column align="center" label="项目编号" prop="project_code" width="160" />
      <el-table-column align="center" label="项目名称" prop="project_name" min-width="200" />
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="发起时间"
        prop="timestamp"
        width="200"
      />
      <el-table-column align="center" label="发起人" prop="initiator_user_name" min-width="120" />
      <el-table-column align="center" label="项目角色" prop="project_role" min-width="120" />
      <el-table-column align="center" label="变更角色" prop="change_role" min-width="120">
        <template #default="scope">
          <div class="flex justify-center">
            <el-tag effect="light" :type="tagTypeObj[scope.row.change_role]"
              >{{ scope.row.change_role }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button link type="primary" @click="handleAudit(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="mt-35px text-14px"
      v-model:current-page="queryParams.pageNo"
      :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      background
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as IctApi from '@/api/project/ict'
import { useRouter } from 'vue-router'
import { ref, reactive, onMounted } from 'vue'

const { push } = useRouter() // 路由
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const Alllist = ref([]) // 总数据
const value = ref(true)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectCode: '',
  projectName: '',
  changeRole: '',
  projectRole: ''
})
const tagTypeObj = {
  我发起的: 'success',
  我参与的: 'warning'
}
const stageList = [
  {
    value: '1',
    label: '我发起的'
  },
  {
    value: '2',
    label: '我参与的'
  }
]
//项目角色
const roleList = [
  {
    value: 'project_receiver',
    label: '项目交底人'
  },
  {
    value: 'construction_project_manager',
    label: '交付经理'
  },
  {
    value: 'construction_department_manager',
    label: '建设单位部门经理'
  },
  {
    value: 'project_supervisor',
    label: '省端项目主管'
  },
  {
    value: 'maintenance_manager',
    label: '服务经理'
  }
]
const queryFormRef = ref()

/** 条数变化 */
const handleSizeChange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}

/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}
/** 数据分页 */
const listcheck = () => {
  list.value = Alllist.value.slice(
    (queryParams.pageNo - 1) * queryParams.pageSize,
    queryParams.pageNo * queryParams.pageSize
  )
}

/** 查询任务列表 */
const getList = async () => {
  const data = {
    projectCode: queryParams.projectCode,
    projectName: queryParams.projectName,
    changeRole: queryParams.changeRole,
    projectRole: queryParams.projectRole
  }
  IctApi.getMyChangePage(data)
    .then((res) => {
      let data = reactive(res)
      Alllist.value = data
      total.value = data.length
      listcheck()
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

/** 搜索按钮操作 */
const handleQuery = async () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.pageNo = 1
  getList()
}

/** 处理审批按钮跳转项目移交详情 */
const handleAudit = async (row: any) => {
  const getProcessId = await IctApi.getAllChangeProcessList({
    project_code: row.project_code,
    change_key_list: [row.change_key]
  })
  push({
    name: 'ProjectChangeApproval',
    query: {
      project_code: row.project_code,
      change_key: row.change_key,
      process_instance_id: getProcessId[0].id
    }
  })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped>
.el-pagination {
  justify-content: flex-end;
}
</style>
